﻿using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Dimok.Data.Database;
using Dimok.Web.UI;

namespace Data.DataSources {
	public class ProfileFiltersDataSource : ObjectDataSourceBase {
		bool showShared = true;
		public bool ShowShared { get { return showShared; } set { showShared = value; } }
		bool showEmpty = true;
		public bool ShowEmpty { get { return showEmpty; } set { showEmpty = value; } }
		static ProfileCommon Profile(string UserName) { return new ProfileCommon().GetProfile(UserName); }
		protected override void OnObjectCreated(object sender, ObjectDataSourceEventArgs e) {
			base.OnObjectCreated(sender, e);
			ProfileFiltersDataSource me = e.ObjectInstance as ProfileFiltersDataSource;
			ShowShared = me.ShowShared;
			ShowEmpty = me.ShowEmpty;
		}
		public DataView SelectEx(string UserName) { return SelectEx(UserName, PageBase.FilterPageType(this)); }
		public DataView SelectEx(string UserName, string FilterPageType) {
			ProfileCommon profile = Profile(UserName);
			GridFilterDS.FilterDataTable t = new GridFilterDS().Filter;
			if (profile.Filters != null) t.DataSet.Merge(profile.Filters.Filter.Select("PageType='"+FilterPageType+"'"));
			if (ShowEmpty) {
				if (t.FindByNamePageType("",FilterPageType) == null) t.AddFilterRow("", "", "","",FilterPageType);
				t.AddFilterRow(" No Filter", " ", "", "", Page.GetType().Name);
				if (profile.UserName.ToLower() != Dimok.User.UserObject.CurrentUserPath.ToLower())
					t.AddFilterRow("**Last Filter**", profile.FilterLast, profile.HideColumnsLast, profile.SortExpressionLast, Page.GetType().Name);
			}
			t = ((GridFilterDS)SQL.SortTable(t, "Name")).Filter;
			if (showShared) MergeShared(UserName,t);
			return t.DefaultView;
		}
		void MergeShared(string UserName,GridFilterDS.FilterDataTable FilterTable) {
			int count = FilterTable.Count;
			foreach (ProfileInfo pi in ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All)) {
				if (UserName.ToLower() == pi.UserName.ToLower()) continue;
				foreach (GridFilterDS.FilterRow row in Profile(pi.UserName).Filters.Filter.Select("Name LIKE 's:%' AND PageType='"+PageBase.FilterPageType(this)+"'","Name"))
					FilterTable.AddFilterRow(Regex.Replace(row.Name, "^s:", "S:") + " {" + Lib.GetUserName(pi.UserName) + "}", row.Value, row.HideColumns, row.SortExpression, Page.GetType().Name);
			}
			if (count < FilterTable.Count) {
				int len = 0;
				foreach (GridFilterDS.FilterRow row in FilterTable)
					len = Math.Max(len, row.Name.Length);
				GridFilterDS.FilterRow rowShared = FilterTable.NewFilterRow();
				rowShared.Name = "".PadLeft(Convert.ToInt32(len * 1.3), '-');
				FilterTable.Rows.InsertAt(rowShared, count);
			}
		}
		public void DeleteEx(string UserName, string Name) {
			ProfileCommon p = Profile(UserName);
			GridFilterDS dsFilters = p.Filters;
			dsFilters.Filter.FindByNamePageType(Name, PageBase.FilterPageType(this)).Delete();
			dsFilters.AcceptChanges();
			p.Save();
		}
		public static void Copy(string userFrom, string userTo, string filterName,Control control) {
			ProfileCommon profileTo = Profile(userTo);
			GridFilterDS.FilterDataTable filterTo = profileTo.Filters.Filter;
			GridFilterDS.FilterDataTable filterFrom = Profile(userFrom).Filters.Filter;
			GridFilterDS.FilterRow rowFrom = filterFrom.FindByNamePageType(filterName, PageBase.FilterPageType(control));
			GridFilterDS.FilterRow rowTo = filterTo.FindByNamePageType(filterName, PageBase.FilterPageType(control));
			if (rowTo == null) filterTo.Rows.Add(rowFrom.ItemArray);
			else rowTo.ItemArray = rowFrom.ItemArray;
			profileTo.Save();
		}
	}
}